package com.example.music;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.music.Bean.LocalMusicBean;
import com.example.music.Utils.FullScreenDialogFragment;
import com.example.music.Utils.LocalMusicAdapter;
import com.example.music.Utils.MusicPlayer;
import com.example.music.Utils.MusicPlaylist;
import com.example.music.Utils.MyAdapter;
import com.example.music.Utils.songListAdapter;
import com.example.music.Utils.songListItem;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HomeActivity extends AppCompatActivity {
    MusicPlaylist mDatas;                       //    数据源
    private LocalMusicAdapter localMusicAdapter;//适配器
    private MusicPlayer musicPlayer;
    private ImageView profile_btn;
    private ImageView home_search;
    private ImageView center_btn;
    public void init(){
        mDatas = MusicPlaylist.getInstance();   //首页歌曲列表
        musicPlayer = MusicPlayer.getInstance();//获取播放器实例

        profile_btn = findViewById(R.id.profile_btn);
        home_search = findViewById(R.id.home_search);
        center_btn = findViewById(R.id.center_btn);
        ImageView homeFocus = findViewById(R.id.home_btn);
        homeFocus.setImageResource(R.drawable.icon_focus_home);
        ImageView profileLostFocus = findViewById(R.id.profile_btn);
        profileLostFocus.setImageResource(R.drawable.icon_profile);
    }
    public void initListener(Context context){
        profile_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpTo(ProfileActivity.class);
            }
        });

        home_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FullScreenDialogFragment dialogFragment = new FullScreenDialogFragment();
                dialogFragment.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        mDatas.clearPlaylist();
                        mDatas.updatePlaylist(context);
                        localMusicAdapter.notifyDataSetChanged();
                    }
                });
                dialogFragment.show(getSupportFragmentManager(), "FullScreenDialog");
            }
        });

        center_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpTo(MainActivity.class);
            }
        });

    }
    public void initRecycleView(){
        //歌单
        RecyclerView horizontalRecyclerView = findViewById(R.id.horizontalRecyclerView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        horizontalRecyclerView.setLayoutManager(layoutManager);
        //推荐歌单列表
        List<String> yourDataSource = new ArrayList<>();
        // 添加一些示例数据
        yourDataSource.add("Item 1");
        yourDataSource.add("Item 2");
        yourDataSource.add("Item 3");
        yourDataSource.add("Item 4");
        yourDataSource.add("Item 5");
        MyAdapter adapter = new MyAdapter(this,yourDataSource);
        horizontalRecyclerView.setAdapter(adapter);

        //主页歌曲
        RecyclerView SongRecyclerView = findViewById(R.id.SongRecyclerView);
        RecyclerView.LayoutManager layoutManager2 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        SongRecyclerView.setLayoutManager(layoutManager2);

        localMusicAdapter = new LocalMusicAdapter(this,mDatas.getSongs());
        SongRecyclerView.setAdapter(localMusicAdapter);

        // 设置每一项的点击事件
        setEventListener();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        init();
        initListener(this);
        initRecycleView();
        localMusicAdapter.notifyDataSetChanged();
    }

    public void jumpTo(Class context){
        Intent intent = new Intent(HomeActivity.this, context);
        startActivity(intent);
    }

    private void setEventListener() {
        /* 设置每一项的点击事件*/
        localMusicAdapter.setOnItemClickListener(new LocalMusicAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                mDatas.setCurrentSongIndex(position);
                LocalMusicBean musicBean = mDatas.getSongs().get(position);
                musicPlayer.addToPlaybackHistory(musicBean);
                musicPlayer.stop();
                musicPlayer.play(musicBean.getPath());
            }
        });
    }

}